import os

def add_word(dictionary):
    word = input("请输入要添加的英文单词：")
    if word.lower() in dictionary:
        print("该单词已添加到字典库")
    else:
        translation = input("请输入该单词的中文释义：")
        dictionary[word.lower()] = translation
        print("单词添加成功")

def search_word(dictionary):
    word = input("请输入要查询的英文单词：")
    if word.lower() in dictionary:
        print("中文释义：", dictionary[word.lower()])
    else:
        print("字典库中未找到这个单词")

def main():
    file_name = "./asssets/dictionary.txt"
    dictionary = {}

    if os.path.exists(file_name):
        with open(file_name, "r") as file:
            for line in file:
                line = line.strip()
                word, translation = line.split(" ")
                dictionary[word.lower()] = translation

    while True:
        print("欢迎使用英文学习词典！请选择操作：")
        print("1. 添加单词")
        print("2. 查询单词")
        print("3. 退出")

        choice = input("请输入选项编号：")
        if choice == "1":
            add_word(dictionary)
        elif choice == "2":
            search_word(dictionary)
        elif choice == "3":
            with open(file_name, "w") as file:
                for word, translation in dictionary.items():
                    file.write(word + " " + translation + "\n")
            print("词典已保存，感谢使用！")
            break
        else:
            print("输入有误，请重新输入选项编号")

main()
